package by.demchenko.sample.webapp.page.home.panel;

import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.enums.ScheduleStatus;
import by.demchenko.sample.services.ScheduleService;
import by.demchenko.sample.services.UserService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.cards.CourseCard;
import by.demchenko.sample.webapp.page.home.share.CoursesPage;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.List;

public class StudentCoursesPanel extends Panel {

    @Inject
    ScheduleService scheduleService;

    @Inject
    UserService userService;

    private Person student;

    @Override
    protected void onInitialize() {
        super.onInitialize();

        if (student == null) {
            return;
        }

        final List<Schedule> schedules = student.getSchedules();

        RepeatingView repeating = new RepeatingView("listStudentCourses");
        add(repeating);

        for (Schedule schedule : schedules) {
            AbstractItem item = new AbstractItem(repeating.newChildId());
            repeating.add(item);

            Link linkCourse = new Link("courseStudent") {
                @Override
                public void onClick() {
                    setResponsePage(new CourseCard(schedule.getCourse()));
                }
            };
            linkCourse.add(new Label("course_textStudent", schedule.getCourse().getName()));
            item.add(linkCourse);

            SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
            item.add(new Label("registrationsStudent", DATE_FORMAT.format(schedule.getStartDate())));

            AreYouSurePanel yesNoPanel = new AreYouSurePanel("yesNoPanel", getString("registration.cancel"), getString("registration.cancel.question")) {
                @Override
                protected void onConfirm(AjaxRequestTarget target) {
                    if (!userService.cancelCourse(BasicAuthenticationSession.get().getUser(), schedule)) {
                        error("can't cancel!");
                    }
                    setResponsePage(new CoursesPage());
                }

                @Override
                protected void onCancel(AjaxRequestTarget target) { }

            };

            item.add(yesNoPanel);

            if (schedule.getStatus() != ScheduleStatus.OPENED) {
                yesNoPanel.setVisible(false);
            }

        }
    }

    public StudentCoursesPanel(String id, Person student) {
        super(id);
        this.student = student;
    }
}
